<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Auth;
class LoginController extends Controller{
 public function index(){
        $this->display();
    }
/**
 * [login 登录页显示]
 * @return [type] [description]
 */
    public function login(){
        $this->display();
    }
/**
 * [logout 登出系统]
 * @return [json] [josn标准数据]
 */
    public function logout(){
    	  if($_SESSION[C('USER_AUTH_KEY')]) {
            session('[destroy]');
            echo json_encode(array('status'=>1,'mes'=>'成功登出系统！'));
          
        }else {
            echo json_encode(array('status'=>0,'mes'=>'登出系统失败！'));
        }
    
       
    }
/**
 * [changepw 修改密码]
 * @return [type] [description]
 */
    public function  changepw(){
        $User = M("User"); 
        $id=I("id",0,"intval");
        $map['id']=$id;
        if(IS_POST){     
            if($id){            
                    $data['password']=I("xpassword1","","md5");
                    $pd=I("ypassword","","md5");
                    $sqlPd=$result=$User->where($map)->getField('password');
                    if($pd!=$sqlPd){
                        echo json_encode(array('status'=>0,'mes'=>'原密码不正确'));
                    }else{
                        $result=$User->where($map)->save($data);
                if($result){
                    echo json_encode(array('status'=>1,'mes'=>'修改密码成功!'));
                }else{
                    echo json_encode(array('status'=>0,'mes'=>'修改密码失败…'));
                }
                    }

                
            }
        
        }

    }

/**
 * [checkLogin 验证登陆表单]
 * @return [type] [description]
 */
    public function checkLogin(){
        $username=I('username','');
        $password=I('password','');
        //$verify_code=I('verify','');
        if($username==''||$password==''){
            $this->redirect("Index/login");

            $data['status']=0;
            $data['mes']="请输入用户名，密码！";
            echo json_encode($data);
            return;
        }
        /*
        if(!$this->_verifyCheck($verify_code)){
            $this->error("验证码错误！！！");
        }
         * */
        // 获得用户信息
        $user=M('user')->where(array('username'=>$username))->find();
        if(!$user||md5($password)!=$user['password']){
            $data['status']=0;
            $data['mes']="用户名或密码错误！！！";
            echo json_encode($data);
            return;
          
        }
        if(!$user['status']){   //status为0时表示锁定
            
            $data['status']=0;
            $data['mes']="用户被锁定！！！";
            echo json_encode($data);
           
        }else{
            $data['login_ip'] =  get_client_ip();
            $data['last_login_time']=date('y-m-d h:i:s',time());
            if(M("user")->where(array('id'=>$user['id']))->save($data)){
                M("user")->where(array('id'=>$user['id']))->setInc("login_num");
            }

            $auth=new Auth();
            $group=$auth->getGroups($user['id']);

            session(C('USER_AUTH_KEY'),$user['id']);//写入权限控制session
            $zname=array();
            $group_id=array();
            foreach ($group as $key => $value) {
                $zname[]=$group[$key]['title'];
                $group_id[]=$group[$key]['group_id'];
            }
            $znames=implode(',', $zname);
            $groups_id=implode(',', $group_id);

            session('zname',$znames);
            session('uid',$user['id']);
            session('uname',$user['username']);
            session('unickname',$user['nickname']);
            session('group_id',$groups_id);
            $data['status']=1;
            session('avatar',$user['avatar']);
            session('sign',$user['sign']);
            $data['mes']="登录成功!!!";
            echo json_encode($data);

        }
    }

public function sss(){
   

    if(in_array('1',explode(",",session('group_id')))){
        echo "asd";
       }else{
        echo "bad";
       }
}


    //验证码
    public function verify(){
        $config = array(    
            'fontSize'    =>    20,     // 验证码字体大小    
            'length'      =>    1,      // 验证码位数    
            'useNoise'    =>   false,  // 关闭验证码杂点
            'imageH'    =>  50,          // 验证码图片高度
            'imageW'    =>  200,          // 验证码图片宽度
        );
        $Verify =new \Think\Verify($config);
        $Verify->entry();
    }

	
}


?>